﻿using System;
using System.Data.Entity;
using System.Data;


namespace StudentInfo.Areas.StudentArea.DAL
{
    public class Repository<EntityObject> : IRepository<EntityObject>
         where EntityObject : class
    {
        protected DbContext context = null;
        public Repository()
        {
            context = new StudentInfoEntities();
        }

        protected DbSet<EntityObject> DbSet
        {
            get
            {
                return context.Set<EntityObject>();

            }
        }

        
        public EntityObject Create(EntityObject obj)
        {
            var newEntry = DbSet.Add(obj);
            context.SaveChanges();
            return newEntry;

        }

        public int Update(EntityObject obj)
        {
            var entry = context.Entry(obj);
            DbSet.Attach(obj);
            entry.State = EntityState.Modified;
            return context.SaveChanges();
        }


        public void Delete(EntityObject obj)
        {
            var entry = context.Entry(obj);
            entry.State = EntityState.Deleted;
            context.SaveChanges();
        }


        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }


        protected virtual void Dispose(bool disposing)
        { 
             if (disposing)
             { 
                 if (context!=null)
                 {
                     context.Dispose();
                     context = null;
                 }
             }
        }
             


            


    }
}

     
